from django.core.validators import RegexValidator
from django.db import models


class ActiveBaseModel(models.Model):
    active = models.BooleanField(verbose_name="状态", default=True, choices=((True, "激活"), (False, "删除")))

    class Meta(object):
        abstract = True


class Administrator(ActiveBaseModel):
    """ 管理员表 """
    username = models.CharField(verbose_name="用户名", max_length=32, db_index=True)
    password = models.CharField(verbose_name="密码", max_length=64)
    mobile = models.CharField(verbose_name="手机号", max_length=11, db_index=True)
    email = models.EmailField(verbose_name="邮箱", db_index=True)

    create_date = models.DateTimeField(verbose_name="创建日期", auto_now_add=True)


class Level(ActiveBaseModel):
    """ 级别表 """
    title = models.CharField(verbose_name="标题", max_length=32)
    percent = models.IntegerField(verbose_name="折扣", default=100,
                                  help_text='输入0-100的整数表示百分比折扣，例如：90，表示90%')

    def __str__(self):
        return self.title


class Customer(ActiveBaseModel):
    """ 客户表 """
    username = models.CharField(verbose_name="用户名", max_length=32, db_index=True)
    password = models.CharField(verbose_name="密码", max_length=64)
    mobile = models.CharField(verbose_name="手机号", max_length=11, db_index=True,
                              validators=[RegexValidator(r'^1[3-9]\d{9}$'), ])
    email = models.EmailField(verbose_name="邮箱", db_index=True)
    balance = models.DecimalField(verbose_name="账户余额", max_digits=10, decimal_places=2, default=0)
    level = models.ForeignKey(Level, verbose_name="级别",
                              on_delete=models.CASCADE)  # , limit_choices_to={'active': True})
    create_date = models.DateTimeField(verbose_name="创建日期", auto_now_add=True)
    creator = models.ForeignKey(Administrator, verbose_name="创建者", on_delete=models.CASCADE)


class PricePolicy(models.Model):
    """ 价格策略（可根据客户级别和购买数量设置折扣） """
    count = models.IntegerField(verbose_name="数量")
    price = models.DecimalField(verbose_name="价格", max_digits=10, decimal_places=2, default=0)


class Order(ActiveBaseModel):
    """ 订单表 """
    status_choices = (
        (1, "待执行"),
        (2, "正在执行"),
        (3, "已完成"),
        (4, "失败"),
        (5, "已撤单"),
    )
    status = models.SmallIntegerField(choices=status_choices, verbose_name="状态", default=1)

    oid = models.CharField(verbose_name="订单号", max_length=64, unique=True)
    url = models.URLField(verbose_name="视频地址", db_index=True)
    count = models.IntegerField(verbose_name="购买数量", default=0)
    # 按照购买数量打折后的价格
    price = models.DecimalField(verbose_name="价格", max_digits=10, decimal_places=2, default=0)
    # 实际价格，按照会员级别打折后的价格，折上折
    real_price = models.DecimalField(verbose_name="实际价格", max_digits=10, decimal_places=2, default=0)

    old_view_count = models.CharField(verbose_name="原播放量", max_length=32, default="0")

    create_date = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    customer = models.ForeignKey(Customer, verbose_name="客户", on_delete=models.CASCADE)
    memo = models.TextField(verbose_name="备注", blank=True, null=True)


class TransactionRecord(ActiveBaseModel):
    """ 交易记录（充值、消费） """
    charge_type_class_mapping = {
        1: "success",
        2: "danger",
        3: "default",
        4: "info",
        5: "primary",
    }  # 搭配bootstrap样式使用
    charge_type_choices = (
        (1, "充值"),
        (2, "扣款"),
        (3, "创建订单"),
        (4, "删除订单"),
        (5, "撤单"),
    )
    charge_type = models.SmallIntegerField(choices=charge_type_choices, verbose_name="类型")

    customer = models.ForeignKey(Customer, verbose_name="客户", on_delete=models.CASCADE)
    amount = models.DecimalField(verbose_name="金额", max_digits=10, decimal_places=2)

    creator = models.ForeignKey(Administrator, verbose_name="管理员", on_delete=models.CASCADE, null=True, blank=True)
    order_oid = models.CharField(verbose_name="订单号", max_length=64, null=True, blank=True, db_index=True)
    create_time = models.DateTimeField(verbose_name="交易时间", auto_now_add=True)
    memo = models.TextField(verbose_name="备注", blank=True, null=True)
